﻿using System;
using System.Windows;
using System.Windows.Threading;

namespace LogoFX.Controls
{
    public class Notification : DependencyObject
    {
        #region Fields

        private static readonly Type s_thisType = typeof(Notification);

        private readonly TimeSpan _timerInterval;

        private readonly DispatcherTimer _timer;

        #endregion

        #region Constructors

        public Notification(object cookie, object dataObject, TimeSpan timeToRelease)
        {
            _timerInterval = timeToRelease;

            Cookie = cookie;
            DataObject = dataObject;

            _timer = new DispatcherTimer();
            _timer.Tick += TimerTick;

            StartTimer();
        }

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty DataObjectProperty =
            DependencyProperty.Register(
                "DataObject",
                typeof(object),
                s_thisType,
                new PropertyMetadata(
                    null,
                    DataObject_Changed));

        public static readonly DependencyProperty PinedProperty =
            DependencyProperty.Register(
                "Pined",
                typeof(bool),
                s_thisType,
                new PropertyMetadata(
                    false,
                    PinedChanged));

        #endregion

        #region Public Properties

        public object DataObject
        {
            get { return GetValue(DataObjectProperty); }
            set { SetValue(DataObjectProperty, value); }
        }

        public bool Pined
        {
            get { return (bool)GetValue(PinedProperty); }
            set { SetValue(PinedProperty, value); }
        }

        public object Cookie { get; private set; }

        #endregion

        #region Public Methods

        public void StopTimer()
        {
            if (Pined)
            {
                return;
            }

            _timer.Stop();
        }

        public void StartTimer()
        {
            if (Pined)
            {
                return;
            }

            _timer.Interval = _timerInterval;
            _timer.Start();
        }

        #endregion

        #region Private Members

        private void TimerTick(object sender, EventArgs e)
        {
            if (Pined)
            {
                return;
            }

            StopTimer();
            NotificationsService.Instance.StartHide(Cookie);
        }

        private static void PinedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Notification notification = ((Notification)d);

            if ((bool)e.NewValue)
            {
                notification.StopTimer();
            }
            else
            {
                notification.StartTimer();
            }
        }

        private static void DataObject_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Notification notification = ((Notification)d);

            if (notification._timer == null)
            {
                return;
            }

            notification.StopTimer();
            notification.StartTimer();
        }

        #endregion
    }
}
